package com.fang.fen.blog.common.utils

import com.fang.fen.blog.common.exception.SystemErrorException
import org.apache.commons.beanutils.PropertyUtils


object BeanUtils {


    /**
     * Copy properties of orig to dest Exception the Entity and Collection Type
     *
     * @param dest
     * @param orig
     * @return the dest bean
     */
    fun copyProperties(dest: Any?, orig: Any?): Any? {

        dest ?: return dest;
        orig ?: return dest;

        val destDesc = PropertyUtils.getPropertyDescriptors(dest)
        try {
            for (i in destDesc.indices) {
                val destType = destDesc[i].propertyType
                val origType = PropertyUtils.getPropertyType(orig, destDesc[i].name)
                if (destType != null && destType == origType && destType != Class::class.java) {
                    if (!Collection::class.java.isAssignableFrom(origType)) {
                        try {
                            val value = PropertyUtils.getProperty(orig, destDesc[i].name)
                            PropertyUtils.setProperty(dest, destDesc[i].name, value)
                        } catch (ex: Exception) {
                        }

                    }
                }
            }

            return dest
        } catch (ex: Exception) {
            throw SystemErrorException(ex)
            // return dest;
        }

    }

    /**    */
    /**
     * Copy properties of orig to dest Exception the Entity and Collection Type
     *
     * @param dest
     * @param orig
     * @param ignores
     * @return the dest bean
     */
    fun copyProperties(dest: Any?, orig: Any?, ignores: Array<String>): Any? {

        dest ?: return dest;
        orig ?: return dest;

        val destDesc = PropertyUtils.getPropertyDescriptors(dest)
        try {
            for (i in destDesc.indices) {
                if (contains(ignores, destDesc[i].name)) {
                    continue
                }

                val destType = destDesc[i].propertyType
                val origType = PropertyUtils.getPropertyType(orig, destDesc[i].name)
                if (destType != null && destType == origType && destType != Class::class.java) {
                    if (!Collection::class.java.isAssignableFrom(origType)) {
                        val value = PropertyUtils.getProperty(orig, destDesc[i].name)
                        PropertyUtils.setProperty(dest, destDesc[i].name, value)
                    }
                }
            }

            return dest
        } catch (ex: Exception) {
            throw SystemErrorException(ex)
        }

    }

    private fun contains(ignores: Array<String>?, name: String): Boolean {
        var ignored = false
        var j = 0
        ignores?.let {

            while (j < ignores.size) {
                if (ignores[j] == name) {
                    ignored = true
                    break
                }
                j++
            }
        }


        return ignored
    }

    fun sum(): Long {
        var sum: Long = 0L
        for (i in 0..Integer.MAX_VALUE)
            sum += i
        return sum
    }

}

fun main(args: Array<String>) {
    println(System.currentTimeMillis());
    BeanUtils.sum();
    println(System.currentTimeMillis());

}
